﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainModel;
using Utils;
using WebUI.Utils;

namespace WebUI.Models
{
    public class JobViewModel : BaseViewModel
    {
        public JobViewModel(int jobID)
            : base(new { JobID = jobID })
        { }

        public Job Job { get; set; }
        public Co Co { get; set; }

        public string AvatarUrl { get; set; }

        //share linkes
        public string ShareToFriendUrl { get; set; }
        public string ShareToTwitterUrl { get; set; }
        public string ShareToFacebookUrl { get; set; }

        //valid page or not
        public Boolean IsValidPage { get; set; }
        //Is this job saved by current login user
        public Boolean IsJobSaved { get; set; }

        protected override void SetVarsWhichDependOnDynamicParams(dynamic dynParams)
        {
            int jobID = dynParams.JobID;
            CoJob coJob = DataUtils.GetCoJobByJobID(jobID);
            Job = coJob.Job;
            Co = coJob.Co;

            IsValidPage = Job != null && Job.JobID > 0 && Co != null && Co.CoID > 0;

            if (IsValidPage)
            {
                Job.JobExp = GlobalUtilities.GetInstance().GetJobExpDName(Job.JobExpID);
                Job.JobType = GlobalUtilities.GetInstance().GetJobTypeDName(Job.JobTypeID);
                AvatarUrl = ImageUtils.GetAvatarRelativeUrl(AvatarType.CoAvatar, Co.CoID, AvatarSizeEnum.Avatar_80x80, Co.AvatarFileExtension);

                var sharetitle = Job.Title.ToUpperAllFirstLetter() + " at " + Co.Name;
                ShareToFriendUrl = CommonUtilities.GetShareToFriendUrlString(CurrentAbsoluteUrl, sharetitle);
                ShareToTwitterUrl = CommonUtilities.GetShareToTwitterUrlString(CurrentAbsoluteUrl, sharetitle);
                ShareToFacebookUrl = CommonUtilities.GetShareToFacebookUrlString(CurrentAbsoluteUrl, sharetitle);
                if (Identity.IsLoggedIn)
                {
                    SavedJob savedJob = DataUtils.GetOne<SavedJob>(ConstUtils.SP_GetOneSavedJob, new { Identity.LoginID, Job.JobID });
                    IsJobSaved = savedJob != null && savedJob.SavedJobID > 0;
                }
                else
                {
                    IsJobSaved = false;
                }
            }

        }
    }
}